/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdVerUsuarios.java
 *
 * Created on 13-ago-2010, 10:15:51
 */

package gestionseguridadadministrativacliente;

import administracion.delegate.GestionSeguridadDelegate;
import administracion.helper.MiTablePrint;
import administracion.helper.Utiles_MensajesDialogo;
import administracion.helper.Utiles_Tabla;
import com.jidesoft.hints.ListDataIntelliHints;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import seguridad.administracion.ejb30.TO.UsuarioTO;


/**
 *
 * @author Luis_Angel
 */
public class jdVerUsuarios extends javax.swing.JDialog {

     private JTableBinding jTableBinding;
     private List<UsuarioTO> trabajadores;
     private List<UsuarioTO> trabajadores_temp;
    private BindingGroup bindingGroup;
    private ListDataIntelliHints intelliHintsTrabajador;
    private jdRegistrarUsuario jdPadre;
    /** Creates new form jdVerUsuarios */
    public jdVerUsuarios(java.awt.Frame parent, boolean modal, jdRegistrarUsuario padre) {
        super(parent, modal);
        this.jdPadre = padre;
        trabajadores_temp = new ArrayList(1);
        initComponents();
        try {
            this.trabajadores = GestionSeguridadDelegate.getInstance().getListaTrabajadores();
            this.actualizaTablaTrabajadores(this.trabajadores);
            this.cargaHintsIndependientes();
        } catch (Exception ex) {
            Logger.getLogger(jdVerUsuarios.class.getName()).log(Level.SEVERE, null, ex);
        }
        pack();
    }

    private void cargaHintsIndependientes() throws Exception {
         this.intelliHintsTrabajador = new ListDataIntelliHints(this.jtfTrabajador,trabajadores);
    }

    private UsuarioTO getTrabajadorcodigo(String dni){
         UsuarioTO retorno = null;
        for (UsuarioTO user : this.trabajadores) {
          if (user.getDni().equalsIgnoreCase(dni)) {
            retorno = user;
            break;
          }
        }
        return retorno;
    }

    private void buscarTrabajador()
      {
        UsuarioTO pro;
        if (this.jtfDni.getText().trim().length() < 1) {
          try { pro = (UsuarioTO)this.intelliHintsTrabajador.getSelectedHint();
          } catch (NullPointerException ex) {
            pro = null;
          }
        } else {
          pro = this.getTrabajadorcodigo(this.jtfDni.getText().trim());
        }
        if (pro == null) {
          Utiles_MensajesDialogo.mensajeError("El trabajador no existe!");
          this.jtfDni.setText("");
          pro = null;
        } else {
          this.jtfTrabajador.setText(pro.getNombre()+ " - " + pro.getDni());
          this.jtfDni.setText(pro.getDni());
          this.trabajadores_temp.clear();
          this.trabajadores_temp.add(pro);
          this.actualizaTablaTrabajadores(this.trabajadores_temp);
       //   this.ruc_proveedor=pro.getCodProveedor();
            try {
           //     this.detalles_solicitud = GestionPedidosDelegate.getInstance().getDetallesSolicitud(pro.getCodProveedor(), this.nro_solicitud);
           //     actualizaTablaDetallesSolicitud();
            } catch (Exception ex) {
                Logger.getLogger(jdVerUsuarios.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

     private void actualizaTablaTrabajadores(List<UsuarioTO> trabajadores_find) {
            this.bindingGroup = new BindingGroup();
            this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, trabajadores_find, this.jtTrabajadores);

            JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${dni}"));

            columnBinding0.setColumnName("DNI");
            columnBinding0.setColumnClass(String.class);
            columnBinding0.setEditable(false);

            JTableBinding.ColumnBinding columnBinding1= this.jTableBinding.addColumnBinding(ELProperty.create("${nombre}"));

            columnBinding1.setColumnName("TRABAJADOR");
            columnBinding1.setColumnClass(String.class);
            columnBinding1.setEditable(false);


            JTableBinding.ColumnBinding columnBinding2 = this.jTableBinding.addColumnBinding(ELProperty.create("${cod_zona}"));

            columnBinding2.setColumnName("ZONA");
            columnBinding2.setColumnClass(String.class);
            columnBinding2.setEditable(false);

            JTableBinding.ColumnBinding columnBinding3 = this.jTableBinding.addColumnBinding(ELProperty.create("${unid_organizativa}"));

            columnBinding3.setColumnName("UNIDAD ORGANIZATIVA");
            columnBinding3.setColumnClass(String.class);
            columnBinding3.setEditable(false);

            this.bindingGroup.addBinding(this.jTableBinding);
            this.bindingGroup.bind();
            int[] porcentajes= {20,50,15,15};
            try {
                Utiles_Tabla.definirAnchoColumnasJTable(this.jtTrabajadores, porcentajes);
            } catch (Exception ex) {
                Logger.getLogger(jdVerUsuarios.class.getName()).log(Level.SEVERE, null, ex);
            }


        }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtTrabajadores = new javax.swing.JTable();
        jLabel18 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jtfTrabajador = new javax.swing.JTextField();
        jbCancelar = new javax.swing.JButton();
        jbAceptar = new javax.swing.JButton();
        jtfDni = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gestionseguridadadministrativacliente.GestionSeguridadAdministrativaclienteApp.class).getContext().getResourceMap(jdVerUsuarios.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtTrabajadores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "DNI", "Trabajador", "Zona", "Unidad Organizativa"
            }
        ));
        jtTrabajadores.setName("jtTrabajadores"); // NOI18N
        jScrollPane1.setViewportView(jtTrabajadores);

        jLabel18.setFont(resourceMap.getFont("jLabel18.font")); // NOI18N
        jLabel18.setText(resourceMap.getString("jLabel18.text")); // NOI18N
        jLabel18.setName("jLabel18"); // NOI18N

        jLabel7.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/user.png"))); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jtfTrabajador.setFont(resourceMap.getFont("jtfTrabajador.font")); // NOI18N
        jtfTrabajador.setName("jtfTrabajador"); // NOI18N
        jtfTrabajador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfTrabajadorActionPerformed(evt);
            }
        });
        jtfTrabajador.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfTrabajadorKeyPressed(evt);
            }
        });

        jbCancelar.setFont(resourceMap.getFont("jbCancelar.font")); // NOI18N
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText(resourceMap.getString("jbCancelar.text")); // NOI18N
        jbCancelar.setName("jbCancelar"); // NOI18N
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        jbAceptar.setFont(resourceMap.getFont("jbAceptar.font")); // NOI18N
        jbAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_ok.png"))); // NOI18N
        jbAceptar.setMnemonic('A');
        jbAceptar.setText(resourceMap.getString("jbAceptar.text")); // NOI18N
        jbAceptar.setName("jbAceptar"); // NOI18N
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });
        jbAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbAceptarKeyPressed(evt);
            }
        });

        jtfDni.setText(resourceMap.getString("jtfDni.text")); // NOI18N
        jtfDni.setName("jtfDni"); // NOI18N
        jtfDni.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfDniActionPerformed(evt);
            }
        });
        jtfDni.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDniKeyPressed(evt);
            }
        });

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel18)
                        .addGap(18, 18, 18)
                        .addComponent(jtfDni, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(jtfTrabajador, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 626, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jbAceptar)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbCancelar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel18))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jtfDni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel7))
                    .addComponent(jtfTrabajador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbAceptar)
                    .addComponent(jButton1)
                    .addComponent(jbCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtfTrabajadorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfTrabajadorActionPerformed

        this.buscarTrabajador();
    }//GEN-LAST:event_jtfTrabajadorActionPerformed

    private void jtfTrabajadorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfTrabajadorKeyPressed
        if (this.jtfDni.getText().trim().length() > 0)
            this.jtfDni.setText("");
}//GEN-LAST:event_jtfTrabajadorKeyPressed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed

        this.dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
         String cod_pedido = "";
         int filaElegida = this.jtTrabajadores.getSelectedRow();

         if (filaElegida < 0) {
              Utiles_MensajesDialogo.mensajeError("Debe elegir un Trabajador!");
              this.jtTrabajadores.requestFocus();
              return;
          }
         else{
              UsuarioTO trabajador  = (UsuarioTO)this.trabajadores_temp.get(filaElegida);
              this.jdPadre.setUsuario(trabajador);
             this.dispose();
         }
    }//GEN-LAST:event_jbAceptarActionPerformed

    private void jbAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbAceptarKeyPressed
        
}//GEN-LAST:event_jbAceptarKeyPressed

    private void jtfDniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfDniActionPerformed
        
    }//GEN-LAST:event_jtfDniActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
         try {
            this.trabajadores_temp.clear();
            this.trabajadores = GestionSeguridadDelegate.getInstance().getListaTrabajadores();
            this.actualizaTablaTrabajadores(this.trabajadores);
        } catch (Exception ex) {
            Logger.getLogger(jdVerUsuarios.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jtfDniKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDniKeyPressed
        if (this.jtfDni.getText().trim().length() < 1)
            this.jtfDni.requestFocus();
        else
            this.buscarTrabajador();
    }//GEN-LAST:event_jtfDniKeyPressed

    /**
    * @param args the command line arguments
    */
  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JTable jtTrabajadores;
    private javax.swing.JTextField jtfDni;
    private javax.swing.JTextField jtfTrabajador;
    // End of variables declaration//GEN-END:variables

}
